3. User and video model

Model creation is the process of defining a data structure or schema in code that represents how data is organized and stored, typically in a database. Example : E-Commerce Model
This is the Model Link: OnlyTube

1. User Model

The user model contains fields for username, password, email, full name, avatar, cover image, refresh tokens, and watch history, each with specific validation rules, and it includes timestamps for creation and updates.

src/model/user model.js

import mongoose,{Schema} from 'mongoose';
import { Jwt } from 'jsonwebtoken';
import bcrypt from "bcrypt"

const userSchema = mongoose.Schema(
    {

        username: {
            type: String,
            required: [true, "this is required field"],
            unique: true,
            max: [10, "username should not exceed 10 characters"],
            lowercase: true,
            index:true
        },

        email: {
            type: String,
            required: [true, "this is required field"],
            unique: true,
            lowercase: true,
            trim:true

        },

        fullName: {
            type: String,
            required: [true, "this is required field"],
            index:true,
            trim:true

        },
        avatar:{
            type:String, //cloudinary url
            required:true
        },
        coverImage:{
            type:String, //cloudinary url
        },
        watchHistory:[
            {
                type:Schema.Types.ObjectId,

                ref:"Video"
            }
        ],

        password:{

            type:String,

            required:[true,"Password is required"]

        },
        refreshToken:{
            type:String,
        }
    }, { timestamps: true }

)
userSchema.pre("save",async function(next){

    if(!this.isModified("password")) return next();

    this.password=bcrypt.hash(this.password,10)

    next()

})

userSchema.methods.isPasswordCorrect = async function(password){

   return await  bcrypt.compare(password,this.password)

}

userSchema.methods.generateAccessToken=function(){

    return jwt.sign({
        _id:this.id,
        email:this.email,
        username:this.username,
        fullName:this.fullName
    },

    process.env.ACCESS_TOKEN_SECRET,

    {

        expiresIn:process.env.ACCESS_TOKEN_EXPIRY

    }

    )

}

userSchema.methods.generateRefreshToken=function(){

    return jwt.sign({

        _id:this.id,

    },

    process.env.ACCESS_TOKEN_SECRET,

    {
        expiresIn:process.env.ACCESS_TOKEN_EXPIRY
    }

    )

}
const User = mongoose.model("User", userSchema)
export default User

2. Video Model

The Video model contains fields for video file, thumbnail, title, description, duration, view count (defaulted to 0), publish status, and the owner's reference, with timestamps for creation and updates, and it supports pagination through the mongoose-aggregate-paginate-v2 plugin

src/model/video model.js

import mongoose, { Schema } from "mongoose"
import mongooseAggregatePaginate from "mongoose-aggregate-paginate"

const videoSchema = new Schema({

    videoFile: {
        type: String,
        required: true
    },
    thumbnail: {
        type: String,
        required: true
    },
    title: {
        type: String,
        required: true
    },
    duration: {
        type: Number,
        required: true
    },
    views: {
        type: Number,
        default: 0,
    },
    isPublished: {
        type: Boolean,
        default: true
    },
    owner: {
        type: Schema.Types.ObjectId,
        ref: "User"
    }  

}, { timestamps: true })
videoSchema.plugin(mongooseAggregatePaginate)

export const Video = mongoose.model("Video", videoSchema)

this is the folder structure

attachments/Pasted image 20241117212654.png| 300